﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HZ.MBSM.Model;
using HZ.MBSM.DBUtility;

public partial class Agency_UpLoadFile : System.Web.UI.Page
{
   
    Int64 projectid;
    Boolean checkfirst;
    protected void Page_Load(object sender, EventArgs e)
    {
        /// 在此处放置用户代码以初始化页面
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);

        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            if (this.IsPostBack)
            {
                projectid = SysFun.ToInt(SysFun.IsSQL(Request.QueryString["projectid"]));
                if (checkfirst == false)
                    this.SaveImages();
            }
            else
            {
                checkfirst = false;


            }
        }
    }

    private Boolean SaveImages()
    {
        string sql;
        int count=0;
        string LoadFileName="";
        Boolean Useful = false;
        string fileName = "";
        string fileExtension = "";
        ///'遍历File表单元素
        HttpFileCollection files = HttpContext.Current.Request.Files;

        /// '状态信息

        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        strMsg.Append("上传的文件分别是：<hr color=red>");
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                ///'检查文件扩展名字

                HttpPostedFile postedFile = files[iFile];
                //string fileName, fileExtension;
                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                fileExtension = System.IO.Path.GetExtension(fileName);
                if (fileExtension.ToUpper() == ".DOC")
                {
                    Useful = true;
                }
                else 
                {
                    Useful = false ;
                }
                if (fileName != "" && Useful)
                {
                    LoadFileName = fileName;
                    strMsg.Append("上传的文件类型：" + postedFile.ContentType.ToString() + "<br>");
                    strMsg.Append("客户端文件地址：" + postedFile.FileName + "<br>");
                    strMsg.Append("上传文件的文件名：" + fileName + "<br>");
                    strMsg.Append("上传文件的扩展名：" + fileExtension + "<br><hr>");
                    ///'可根据扩展名字的不同保存到不同的文件夹

                    ///注意：可能要修改你的文件夹的匿名写入权限。

                    postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../UploadFile/") + fileName);

                }
            }
            if (fileName != "" && Useful)
            {
                Db db = new Db();
                System.Data.IDataReader dr;
                sql = "select count(*) as cun from project_bulletin where projectid=" + projectid;
                dr = db.DataReader(sql);
                while (dr.Read())
                {
                    count = SysFun.ToInt(dr["cun"].ToString());
                }
                dr.Dispose();
                dr.Close();
                if (count == 0)
                {
                    strStatus.Text = "还没发布招标公告!,不能上传文件";
                    db.Close();
                    checkfirst = true;
                    return false;
                }
                else
                {
                    sql = "update project_bulletin set DownUrl='" + LoadFileName + "' where projectid=" + projectid;
                    db.Command(sql);
                    db.Close();

                     HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                     HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "project_bulletin", sql, "../Agency/UpLoadFile.aspx");

                    strStatus.Text = strMsg.ToString();
                    checkfirst = true;
                    return true;
                }
            }
            else
            {
                strStatus.Text = "只能上传文件格式(.doc)!";
                checkfirst = true;
                return false;

            }
        }
        catch (System.Exception Ex)
        {
            strStatus.Text = Ex.Message;

            return false;
        }
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。

        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改

    /// 此方法的内容。

    /// </summary>
    private void InitializeComponent()
    {
        this.ID = "Upload";
        this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

}

